#include <iostream>
#include <cmath>
#include <vector>
#include <functional>
#include <fstream>
#include "newton_interpolation.hpp"


void NewtonData(const std::string& filename, NewtonInterpolation f) {
    int N = 1000;
    std::vector<double> x, y;  
    //x.clear();  
    //y.clear();  
    std::ofstream datafile(filename);  
    if (datafile.is_open()) {  
        for (int i = 0; i < N; i++) {  
            x.push_back(i * 30. / N);  
            y.push_back(f.interpolate(x[i]));  
            datafile << x[i] << " " << y[i] << std::endl;  
        }  
        datafile.close();  
    } else {  
        std::cerr << "Unable to open file";  
    }  
}  


int main(){
  std::vector<double> x = {0,6,10,13,17,20,28};
  std::vector<double> y1 = {6.67,17.3,42.7,37.3,30.1,29.3,28.7};
  std::vector<double> y2 = {6.67,16.1,18.9,15.0,10.6,9.44,8.89};
  NewtonInterpolation f1 = NewtonInterpolation(x,y1);
  NewtonInterpolation f2 = NewtonInterpolation(x,y2);
  NewtonData("data_1.txt",f1);
  NewtonData("data_2.txt",f2);

  double w1 = f1.interpolate(43);
  double w2 = f2.interpolate(43);
  
  std::cout <<"the average weight of sp1 after 15 days: "<< w1 << std::endl;
  std::cout <<"the average weight of sp2 after 15 days: "<< w2 << std::endl;

  if (w1 > 0)
      std::cout << "sp1 live well" << std::endl;
  else
      std::cout << "sp1 gg" << std::endl;
  if(w2>0)
      std::cout << "sp2 live well" << std::endl;
  else
      std::cout << "sp2 gg" << std::endl;
}
